﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

//***************************************************
//*NOTICE: GamePad functionality not yet implemented*
//***************************************************
namespace JumpAndRun.Input
{
    public class InputManager
    {
        private MouseState currentMouseState = Mouse.GetState();
        private MouseState lastMouseState;
        private KeyboardState currentKeyboardState = Keyboard.GetState();
        private KeyboardState lastKeyboardState;
        private GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
        private GamePadState lastGamePadState;

        public bool IsAnyKeyPressed { get; private set; }

        public Vector2 MousePosition
        {
            get { return new Vector2(this.currentMouseState.X, this.currentMouseState.Y); }
        }

        public bool RightClick
        {
            get
            {
                return this.currentMouseState.RightButton == ButtonState.Released &&
                    this.lastMouseState.RightButton == ButtonState.Pressed;
            }
        }

        public bool LeftClick
        {
            get
            {
                return this.currentMouseState.LeftButton == ButtonState.Released &&
                    this.lastMouseState.LeftButton == ButtonState.Pressed;
            }
        }

        public bool RightMouseButtonPressed
        {
            get { return this.currentMouseState.RightButton == ButtonState.Pressed; }
        }

        public bool LeftMouseButtonPressed
        {
            get { return this.currentMouseState.LeftButton == ButtonState.Pressed; }
        }

        public InputManager() { }

        public void Update()
        {
            this.lastMouseState = this.currentMouseState;
            this.lastKeyboardState = this.currentKeyboardState;
            this.lastGamePadState = this.currentGamePadState;
            this.currentMouseState = Mouse.GetState();
            this.currentKeyboardState = Keyboard.GetState();
            this.currentGamePadState = GamePad.GetState(PlayerIndex.One);

            this.IsAnyKeyPressed = this.currentKeyboardState.GetPressedKeys().Length > 0;
        }

        public bool IsKeyPressed(Keys key)
        {
            return this.currentKeyboardState.IsKeyDown(key);
        }

        public bool IsKeyTapped(Keys key)
        {
            return this.currentKeyboardState.IsKeyUp(key) && this.lastKeyboardState.IsKeyDown(key);
        }
    }
}
